package xyz.yuzegod.megawalls.classes.normal.spider;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.entity.Firework;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.CollectSkill;
import xyz.yuzegod.megawalls.game.ChestManager;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.ItemUtils;
import xyz.yuzegod.megawalls.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class FourthSkill extends CollectSkill
{
    public FourthSkill(final Classes classes) {
        super("寻铁者", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 3;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 0.8;
            }
            case 2: {
                return 0.9;
            }
            case 3: {
                return 1.0;
            }
            default: {
                return 0.8;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7当采集泥土、草、沙子、");
            lore.add("    §7砂砾或菌丝时,");
            lore.add("    §7有§a" + StringUtils.percent(this.getAttribute(level)) + "§7几率获得铁锭。");
            return lore;
        }
        lore.add(" §8▪ §7当采集泥土、草、沙子、");
        lore.add("    §7砂砾或菌丝时,");
        lore.add("    §7有§8" + StringUtils.percent(this.getAttribute(level - 1)) + " ➜ §a" + StringUtils.percent(this.getAttribute(level)) + "§7几率获得铁锭。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkill4Level();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkill4Level();
    }
    
    @Override
    public void onBlockBreak(final KitStatsContainer kitStats, final BlockBreakEvent e) {

        if ((ItemUtils.isDirtResource(e.getBlock())) && MegaWalls.getRandom().nextInt(100) <= this.getAttribute(kitStats.getSkill4Level()) * 100.0) {
            e.getPlayer().getInventory().addItem(new ItemStack(Material.IRON_INGOT));
            if (MegaWalls.getRandom().nextInt(MegaWalls.getInstance().getGame().isWallsFall() ? 1000 : 150) <= 6) {
                e.setCancelled(true);
                final Block block = e.getBlock();
                block.setType(Material.TRAPPED_CHEST);
                GamePlayer.get(e.getPlayer().getUniqueId()).addProtectedBlock(block);
                final Chest chest = (Chest)block.getState();
                block.getWorld().spawn(block.getLocation(), (Class)Firework.class);
                ChestManager.fillInventory(e.getPlayer(), chest.getBlockInventory());
            }
        }
    }
}
